/**
 * 
 */
package org.hotDating.android.individual.mmi;

import java.util.ArrayList;

import org.hotDating.android.individual.module.Friend;
import org.hotDating.android.individual.service.friends.FriendsService;

import android.app.ListActivity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;



/**
 * @author joy
 * 
 */
public class Friends extends ListActivity {

	private ArrayList<Friend> mFriends;
	public ArrayList<Friend> getMFriends() {
		return mFriends;
	}
	public void setMFriends(ArrayList<Friend> friends) {
		mFriends = friends;
	}
	FriendsAdapter mAdapter;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friends);
		getListView().setEmptyView(findViewById(R.id.empty));
		mFriends = FriendsService.getFriendsList();
        mAdapter = new FriendsAdapter(this);
        setListAdapter(mAdapter);
        Frame.setup(this);
	}


 class FriendsAdapter extends BaseAdapter {
	 
	private Context mContext;
	private MyDataSetObserver mObserver;

    private class MyDataSetObserver extends DataSetObserver {
        @Override
        public void onChanged() {
            notifyDataSetChanged();
        }

        @Override
        public void onInvalidated() {
            notifyDataSetInvalidated();
        }
    }
    
    public FriendsAdapter(Context c) {
        mContext = c;
        mObserver = new MyDataSetObserver();
        FriendsService.setObserver(mObserver);
    }

    public int getCount() {
        return mFriends.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        
        View view;
        if (convertView == null) {
            // Make up a new view
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.friend_item, null);
        } else {
            // Use convertView if it is available
            view = convertView;
        }
        Friend f = mFriends.get(position);

        ImageView i = (ImageView) view.findViewById(R.id.friend_item_photo);
        i.setImageBitmap(f.getMPhoto());
        i.setBackgroundResource(R.drawable.picture_frame);
        
        TextView t = (TextView) view.findViewById(R.id.friend_item_name);
        t.setText(f.getMName());
        
        t = (TextView) view.findViewById(R.id.friend_item_message);
        t.setText(f.getMMessage());
        return view;
    }

    

//    public void clearPhotos() {
//        mPhotos.clear();
//        notifyDataSetChanged();
//    }
//    
//    public void addPhotos() {
//        int whichPhoto = (int)Math.round(Math.random() * (mPhotoPool.length - 1));
//        int newPhoto = mPhotoPool[whichPhoto];
//        mPhotos.add(newPhoto);
//        notifyDataSetChanged();
//    }
 }
}

